#pragma once
#include <iostream>
#include <string>
#include <vector>

size_t split(const std::string& str, const std::string& sep, std::vector<std::string>& result)
{
    //   ...news.#.haha.aah.#.    // sep = "."
    // index^   ^pos    ====> 提取子串"news"将其push进result
    size_t pos = 0, index = 0;
    while(index < str.size())
    {
        pos = str.find(sep, index);
        // 如果未找到sep分隔符，取整个字符串push进result容器
        if(pos == std::string::npos)
        {
            result.push_back(str.substr(index));
            break;
        }

        if(pos == index)
        {
            index = pos + sep.size();
            continue;
        }

        // 若截取到新子串，push进result容器，同时index右移到pos位置后（pos + sep.size()）
        result.push_back(str.substr(index, pos - index));
        index = pos + sep.size();
    }
    return result.size();
}